package com.tanhua.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.SettingsMapper;
import com.tanhua.common.pojo.Settings;
import com.tanhua.common.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SettingsServiceImpl implements SettingsService {

    @Autowired
    private SettingsMapper settingsMapper;

    @Override
    public Settings querySettings(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Settings settings = this.settingsMapper.selectOne(queryWrapper);
        return settings ;
    }

    @Override
    public void updateNotification(Long id, Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        Settings settings = this.querySettings(id);

        if (!Optional.ofNullable(settings).isPresent()){
            //在有数据的时候去更新数据
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            this.settingsMapper.updateById(settings);
        }else {
            //在没有数据的时候插入数据保存
            settings = new Settings();
            settings.setUserId(id);
            this.settingsMapper.insert(settings);
        }
    }
}
